package com.qiguliuxing.dts.db.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import com.qiguliuxing.dts.db.dao.DtsOrderGoodsMapper;
import com.qiguliuxing.dts.db.domain.DtsOrderGoods;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DtsOrderGoodsService {
	@Resource
	private DtsOrderGoodsMapper orderGoodsMapper;

	public int add(DtsOrderGoods orderGoods) {
		orderGoods.setAddTime(LocalDateTime.now());
		orderGoods.setUpdateTime(LocalDateTime.now());
		return orderGoodsMapper.insert(orderGoods);
	}

	public List<DtsOrderGoods> queryByOid(Integer orderId) {
		QueryWrapper<DtsOrderGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsOrderGoods::getOrderId, orderId);
		queryWrapper.lambda().eq(DtsOrderGoods::getDeleted, false);
		return orderGoodsMapper.selectList(queryWrapper);
	}

	public List<DtsOrderGoods> findByOidAndGid(Integer orderId, Integer goodsId) {
		QueryWrapper<DtsOrderGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsOrderGoods::getOrderId, orderId);
		queryWrapper.lambda().eq(DtsOrderGoods::getGoodsId, goodsId);
		queryWrapper.lambda().eq(DtsOrderGoods::getDeleted, false);
		return orderGoodsMapper.selectList(queryWrapper);
	}

	public DtsOrderGoods findById(Integer id) {
		return orderGoodsMapper.selectById(id);
	}

	public void updateById(DtsOrderGoods orderGoods) {
		orderGoods.setUpdateTime(LocalDateTime.now());
		orderGoodsMapper.updateById(orderGoods);
	}

	public Short getComments(Integer orderId) {
		QueryWrapper<DtsOrderGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsOrderGoods::getOrderId, orderId);
		queryWrapper.lambda().eq(DtsOrderGoods::getDeleted, false);
		return orderGoodsMapper.selectCount(queryWrapper).shortValue();
	}

	public boolean checkExist(Integer goodsId) {
		QueryWrapper<DtsOrderGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsOrderGoods::getGoodsId, goodsId);
		queryWrapper.lambda().eq(DtsOrderGoods::getDeleted, false);
		return orderGoodsMapper.selectCount(queryWrapper) != 0;
	}
}
